package com.dale.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dale.common.lang.Result;
import com.dale.entity.FriendUrl;
import com.dale.service.FriendUrlService;
import com.dale.util.QiniuUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @author Gyt
 * @create 2021-06-22 - 10:47
 */
@RestController
public class FriendUrlController {

    @Autowired
    private FriendUrlService friendUrlService;


    @PostMapping("/addUrl")
    @RequiresAuthentication
    @RequiresRoles("admin")
    public Result addUrl(@RequestBody FriendUrl friendUrl) {
        // 地址 友情连接的图片来自其站点   自己不用保存到OSS

        if (friendUrl.getFid() == null) {
            //增加url
            friendUrlService.save(friendUrl);
        } else {
            //修改url
            friendUrlService.updateById(friendUrl);
        }


        return Result.succ(null);

    }

//    @PostMapping("/editUrl")
//    @RequiresAuthentication
//    @RequiresRoles("admin")
//    public Result editUrl(@RequestBody FriendUrl friendUrl) {
//
//        friendUrlService.updateById(friendUrl);
//
//        return Result.succ(null);
//
//    }

    @PostMapping("/deleteUrl/{fid}")
    @RequiresAuthentication
    @RequiresRoles("admin")
    public Result deleteUrl(@PathVariable Long fid) {
        friendUrlService.removeById(fid);

        return Result.succ(null);
    }

    @GetMapping("/showUrl")
    public Result showUrl(@RequestParam(defaultValue = "1") Integer currentPage) {

        //MybatisPlus分页  参数(起始页面、个数)
        Page page = new Page(currentPage, 6);  //每页只展示6个站点
        IPage pageData = friendUrlService.page(page);
        return Result.succ(pageData);
    }

    @GetMapping("/getUrlById/{fid}")
    public Result getUrlById(@PathVariable Long fid) {

        return Result.succ(friendUrlService.getById(fid));
    }


}
